package com.electron.power;

import org.smartboot.socket.transport.AioQuickServer;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import com.electron.power.aio.mod.cha.ModCharProcessor;
import com.electron.power.aio.mod.cha.ModCharProtocol;
import com.electron.power.aio.mod.str.FlinkModProcessor;
import com.electron.power.aio.mod.str.FlinkModProtocol;
import com.electron.power.model.mod.ModStr;

@SpringBootApplication
//@EnableBinding(Processor.class)
public class ForgeServer implements DisposableBean, ExitCodeGenerator {
	// public static void main(String[] args) throws IOException {
	// AioQuickServer<Integer> server = new AioQuickServer<Integer>(8888, new
	// ModProtocol(),
	// new IntegerServerProcessor());
	// server.start();
	// }
	private AioQuickServer<ModStr> server;

	private AioQuickServer<String> flinkServer;

	public static void main(String[] args) {
		SpringApplication.run(ForgeServer.class, args);
	}

	@Bean
	public CommandLineRunner initData(ModCharProtocol modProtocol, ModCharProcessor modServerProcessor,
			FlinkModProtocol flinkModProtocol,FlinkModProcessor flinkModProcessor) {
		return (String... args) -> {
			server = new AioQuickServer<ModStr>(8888, modProtocol, modServerProcessor);
			server.start();
			// 开启flink的服务端,准备给flink发送数据
			flinkServer = new AioQuickServer<String>(9000, flinkModProtocol, flinkModProcessor);
			flinkServer.start();
		};
	}

	@Override
	public void destroy() throws Exception {
		if (server != null) {
			server.shutdown();
		}
		if(flinkServer!=null) {
			flinkServer.shutdown();
		}
		System.out.println("<<<<<<<<<<<我被销毁了......................>>>>>>>>>>>>>>>");
	}

	@Override
	public int getExitCode() {
		return 5;
	}

//    @SendTo(Processor.OUTPUT)
//    public Mod enrichLogMessage(Mod log) {
//        return new Mod();
//    }
}
